package com.lh.Mybatis.service;

import com.lh.Mybatis.dao.IEmpDao;
import com.lh.Mybatis.model.Emp;
import com.lh.Mybatis.model.dto.DeptCount;
import com.lh.Mybatis.model.dto.EmpYearSalary;
import com.lh.Mybatis.util.BatchHandleTask;
import com.lh.Mybatis.util.SessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 作者：idea
 * 日期：2018/7/5
 * 描述：
 */
public class EmpService {


    private static  String findAllMethod(){
        return "com.lh.Mybatis.dao.IEmpDao.findAll";
    }

    //查询员工的姓名和年薪(月薪*12)
    private static  String countEmpYearSalaryMethod(){
        return "com.lh.Mybatis.dao.IEmpDao.countEmpYearSalary";
    }

    //查询薪资最高的员工姓名和工资
    private static  String findTopSalaryMethod(){
        return "com.lh.Mybatis.dao.IEmpDao.findTopSalary";
    }

    //查询有员工的部门的编号和人数
    private static  String findDeptNoAndNumsMethod(){
        return "com.lh.Mybatis.dao.IEmpDao.findDeptNoAndNums";
    }

    private static String batchInsertMethod(){
        return "com.lh.Mybatis.dao.IEmpDao.insertBatch";
    }

    public List<Emp> findAll() throws IOException {
        SqlSession sqlSession=SessionFactory.getSqlSession();
        return sqlSession.selectList(findAllMethod());
    }

    //查询员工的姓名和年薪(月薪*12)
    public List<EmpYearSalary> countEmpYearSalary() throws IOException {
        SqlSession sqlSession=SessionFactory.getSqlSession();
        return sqlSession.selectList(countEmpYearSalaryMethod());
    }

    //查询薪资最高的员工姓名和工资
    public  List<Emp> findTopSalary() throws IOException {
        SqlSession sqlSession=SessionFactory.getSqlSession();
        return sqlSession.selectList(findTopSalaryMethod());
    }

    //查询有员工的部门的编号和人数
    public List<DeptCount> findDeptNoAndNums()throws IOException {
        SqlSession sqlSession=SessionFactory.getSqlSession();
        return sqlSession.selectList(findDeptNoAndNumsMethod());
    }

    //大型数据处理器（处理百万级别）
//    public List<Emp> bigNumsHandle(List<Emp> list,int nums){
//        if(nums>500000){//五十万处理
//
//        }
//    }

    public void batchInsert(int nums) throws IOException {
        SqlSession sqlSession=SessionFactory.getBatchSession();
        List<Emp> empList=new ArrayList<Emp>();
        Emp emp;
        for(int i=1;i<nums;i++){
            System.out.println(i);
            emp=new Emp();
            emp.setDeptNo(441511);
            emp.setSalary(5000);
            emp.setFirstName("hahah");
            if(i%10000==0){  //表示每隔10000处理一次，并且插入一次数据
                empList.add(emp);
                sqlSession.insert(batchInsertMethod(),empList);
                sqlSession.commit();
                System.out.println("success");
                empList.clear();
            }else{
                empList.add(emp);
            }
            emp=null;
        }
    }

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        List<Future> futureList=new ArrayList<Future>();
        ExecutorService es=Executors.newCachedThreadPool();
        long begin=System.currentTimeMillis();
        futureList.add(es.submit(new BatchHandleTask(10000))); //执行批处理任务
        long end=System.currentTimeMillis();
        System.out.println("消耗时间--"+(end-begin));
        System.out.println("命令下达结束");

    }
}
